package com.tutu.controller.backend;

import com.tutu.common.DataTableFactory;
import com.tutu.controller.common.CommonController;
import com.tutu.service.CompanyBalanceService;
import com.tutu.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 配额管理
 */
@Controller
@RequestMapping(value = "backend/balance")
public class CompanyBalanceController extends CommonController {

    @Autowired
    CompanyBalanceService companyBalanceService;

    @RequestMapping(value = "/index")
    public String index() {
        return "backend/balance/list";
    }

    @RequestMapping(value = "/child/index")
    public String childIndex(Model model, Integer companyBalanceId) {
        model.addAttribute("companyBalanceId", companyBalanceId);

        return "backend/balance/childList";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response, Integer companyId, String keyWord, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, companyBalanceService.pageByParams(companyId, keyWord, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/child/list")
    public void childList(HttpServletResponse response, Integer companyBalanceId, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, companyBalanceService.pageDataByParams(companyBalanceId, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(HttpServletRequest request, Integer id, Integer companyId, Integer codeDictType, String codeDictName, double count) {
        return companyBalanceService.saveInfo(request, id, companyId, codeDictType, codeDictName, count);
    }

    @RequestMapping("/batchAdd")
    @ResponseBody
    public Map<String, Object> batchAdd(HttpServletRequest request, Integer deviceId, String emissionCodes, String values) {
        return companyBalanceService.batchAdd(request, deviceId, emissionCodes, values);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> del(HttpServletRequest request, Integer id) {
        return companyBalanceService.deleteById(request, id);
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public Map<String, Object> batchDel(HttpServletRequest request, String ids) {
        return companyBalanceService.batchDel(request, ids);
    }

    @RequestMapping("/findBalanceListByCompanyId")
    @ResponseBody
    public Map<String, Object> findBalanceListByCompanyId(Integer companyId) {
        return companyBalanceService.findBalanceListByCompanyId(companyId);
    }

    @RequestMapping("/companyBalanceStatic")
    @ResponseBody
    public Map<String, Object> companyBalanceStatic(HttpServletRequest request) {
        return companyBalanceService.companyBalanceStatic(request);
    }

    @RequestMapping("/child/save")
    @ResponseBody
    public Map<String, Object> childSave(HttpServletRequest request, Integer id, Integer status) {
        return companyBalanceService.saveChildInfo(request, id, status);
    }
}
